//*************************************************************************************
/** \file task_wind.h
 *    This file contains a task which reads a wind sensor. The sensor uses a 
 *    timer/counter in the AVR to add up pulses from the anemometer; to find wind 
 *    speed, the number of pulses counted by the counter is read and compared to the 
 *    number of pulses counted some time ago, giving an average rate of pulses per 
 *    time period. If being used on a particular unit, a wind sensor based on a US
 *    Digital analog "encoder" (a magnetically based potentiometer replacement) is used
 *    to find wind direction; the direction is processed as a pair of trigonometric 
 *    functions, the sine and the cosine of the wind direction angle. Both wind speed 
 *    and wind direction are averaged over time; the average is read (the averaging 
 *    sums automatically being set to zero) periodically by another task. 
 *
 *  Revisions:
 *    \li 12-26-2007 JRR Original file, with wind speed counting working only
 *    \li 12-27-2007 JRR Got the wind direction stuff working
 *    \li 03-20-2009 JRR Removed code for AAG sensors, which aren't reliable enough
 */
//*************************************************************************************

/// This define prevents this file from being included more than once in a *.cc file
#ifndef _TASK_WIND_H_
#define _TASK_WIND_H_

#include "adc_10bit.h"
#include "stl_task.h"


// If there's a voltage divider hooked up to the battery voltage, we can use it to 
// check the battery, in which case this should be defined. If not, it should be
// commented out, and battery voltage won't be measured or sent

#define BATTERY_CHANNEL		5				///< The A/D channel for battery voltage


//-------------------------------------------------------------------------------------
/** This class contains a task which reads a DS1822 temperature sensor periodically
 *  and makes the average reading available to anyone who calls its get_average()
 *  method. 
 */

class task_wind : public stl_task
{
	protected:
		adc_10bit* my_adc;					///< Pointer to the A/D converter driver
		unsigned long previous_count;		///< Previous values from speed counters
		unsigned int most_recent_speed;		///< The readings most recently taken
		unsigned int high_speed;			///< Highest recently read speeds
		unsigned int low_speed;				///< Lowest recently read speed
		char most_recent_sine;				///< The most recent direction sine reading 
		char most_recent_cosine;			///< The most recent direction cosine read
		unsigned char num_readings;			///< Number of readings taken in a cycle
		long sum_of_speeds;					///< The sums of speed readings
		int sum_of_sines;					///< The sum of sines of directions
		int sum_of_cosines;					///< The sum of cosines of directions

	public:
		// The constructor creates a new task object
		task_wind (task_timer&, time_stamp, adc_10bit* = NULL);

		void reset_sums (void);             // Reset max, min, averaging sums

		// The run method is where the task actually performs its function
		char run (char);

		// This method computes the cosine of a 0-255 angle
		signed char cosine (unsigned char);

		// This method computes the sine of a 0-255 angle
		signed char sine (unsigned char);

		// This method returns an A/D conversion of the battery voltage
		unsigned int check_battery (void);

		// This method calculates and returns the average reading since it was 
		// previously called, then restarts the averaging process
		unsigned int get_average_speed (void);

		// This method returns the maximum gust reading
		unsigned int get_maximum_speed (void);

		// This method returns the minimum wind speed reading
		unsigned int get_minimum_speed (void);

		// This method returns the most recent speed reading
		unsigned int get_latest_speed (void);

		// This method gets the most recent direction readings
		void get_latest_direction (char*, char*);

		// This method gets averaged direction readings
		void get_average_direction (char*, char*);
};

#endif // _TASK_WIND_H_
